package com.jblv.frame.web.controller.impl;

import com.jblv.frame.common.annotation.AuthIgnore;
import com.jblv.frame.common.controller.BaseController;
import com.jblv.frame.common.dto.PageResult;
import com.jblv.frame.common.dto.UserTokenDTO;
import com.jblv.frame.common.message.Result;
import com.jblv.frame.user.api.entity.SysUser;
import com.jblv.frame.web.controller.ISysUserController;
import com.jblv.frame.web.facade.ISysUserFacade;
import com.jblv.frame.user.api.dto.SysUserDTO;
import com.jblv.frame.user.api.dto.UserLoginResultDTO;
import com.jblv.frame.user.api.params.UserLoginParams;
import com.jblv.frame.user.api.params.SysUserListParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author happyonion.yangtao
 * @version 1.0
 * @title SysUserController
 * @projectName jblv-frame
 * @packageName com.jblv.web
 * @description ISysUserController 实现类
 * @date Create at 2020/4/22 22:47
 * @since jdk1.8
 */
@Api(tags = "管理员管理", value = "/v1/api/users", description = "关于管理员相关接口")
@RestController
@RequestMapping("/v1/api/users")
public class SysUserController extends BaseController implements ISysUserController {

    @Autowired
    ISysUserFacade userInfoFacade;

    @ApiOperation(value = "获取当前TOKEN管理员信息", response = SysUser.class, httpMethod = "GET", notes = "传入管理员信息")
    @Override
    @GetMapping("/info")
    public Result<UserLoginResultDTO> getInfo(){
        return renderSuccess(userInfoFacade.get());
    }

    @ApiOperation(value = "获取ID管理员信息", response = SysUser.class, httpMethod = "GET", notes = "传入管理员信息ID")
    @Override
    @GetMapping("/id/{id}")
    public Result<SysUser> get(@PathVariable("id") String id){
        return renderSuccess(userInfoFacade.get(id));
    }

    @ApiOperation(value = "获取管理员列表", httpMethod = "POST", notes = "获取管理员列表说明")
    @Override
    @PostMapping("/list")
    public Result<PageResult<SysUserDTO>> getList(@RequestBody SysUserListParams params){
        return renderSuccess(userInfoFacade.getList(params));
    }

    @ApiOperation(value = "创建管理员", httpMethod = "POST", notes = "传入管理员DTO")
    @Override
    @PostMapping("")
    public Result<String> save(@RequestBody SysUserDTO sysUserDTO) {
        return renderSuccess(userInfoFacade.create(sysUserDTO));
    }

    @ApiOperation(value = "删除管理员", httpMethod = "DELETE", notes = "传入管理员ID")
    @Override
    @DeleteMapping("/{id}")
    public Result<Boolean> del(@PathVariable String id) {
        return renderSuccess(userInfoFacade.del(id));
    }

    @ApiOperation(value = "更新管理员", httpMethod = "PUT", notes = "传入管理员DTO")
    @Override
    @PutMapping("")
    public Result<Boolean> update(@RequestBody SysUserDTO sysUserDTO) {
        return renderSuccess(userInfoFacade.update(sysUserDTO));
    }

    @ApiOperation(value = "更新管理员状态", httpMethod = "PUT", notes = "传入管理员ID和状态")
    @Override
    @PutMapping("/status/{id}/{status}")
    public Result<Boolean> status(@PathVariable("id") String id, @PathVariable("status") Integer status) {
        return renderSuccess(userInfoFacade.status(id,status));
    }

    @ApiOperation(value = "管理员登录", httpMethod = "POST", notes = "传入管理员登录参数")
    @Override
    @PostMapping("/login")
    @AuthIgnore
    public Result<UserTokenDTO> pwdLogin(@RequestBody  UserLoginParams params) {
        return renderSuccess(userInfoFacade.pwdLogin(params));
    }

    @ApiOperation(value = "管理员退出登录", httpMethod = "POST", notes = "")
    @Override
    @PostMapping("/logout")
    public Result<Boolean> logout() {
        return renderSuccess(userInfoFacade.logout());
    }
}
